package com.sugree.thairath.breakingnews.entity;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.util.Log;

public class Message implements Comparable<Message> {
	public final static String TAG = "Message";

	protected final static SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);

	protected String mAuthor;
	protected String mTitle;
	protected URL mLink;
	protected String mDescription;
	protected Date mPubDate;

	@Override
	public String toString() {
		return getLink();
	}

	@Override
	public int hashCode() {
		return mLink.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return mLink.toString().equals(((Message)obj).getLink());
	}

	public int compareTo(Message another) {
		if (another == null) {
			return 1;
		}
		return another.mPubDate.compareTo(mPubDate);
	}

	public String getAuthor() {
		return mAuthor;
	}

	public void setAuthor(String author) {
		mAuthor = author;
	}

	public String getTitle() {
		return mTitle;
	}

	public void setTitle(String title) {
		mTitle = title;
	}

	public String getLink() {
		return mLink.toString();
	}

	public void setLink(String link) {
		try {
			mLink = new URL(link);
		} catch (MalformedURLException e) {
			Log.e(TAG, e.getMessage()+": "+link);
			throw new RuntimeException(e);
		}
	}

	public String getDescription() {
		return mDescription;
	}

	public void setDescription(String description) {
		mDescription = description;
	}

	public String getPubDate() {
		return FORMATTER.format(mPubDate);
	}

	public void setPubDate(String pubDate) {
		pubDate = pubDate.replace("GMT", "+0000");
		while (!pubDate.endsWith("00")){
			pubDate += "0";
		}
		try {
			mPubDate = FORMATTER.parse(pubDate.trim());
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
}
